using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using BenevolentSun;

namespace Editor
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Editor : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        ScreenManager screenManager;


        public static string LevelsSourcePath;
        public static string LevelsOutputPath;

        public static List<string> LevelContentNames = new List<string>();

        public static List<string> LevelNames = new List<string>();

        public static string GetLevelContentName(string levelName)
        {
            return "Maps\\" + levelName;
        }

        public static string GetLevelSourceFile(string contentName)
        {
            var fileName = contentName.Replace("Maps\\", "") + ".xml";
            return LevelsSourcePath + fileName;
        }
        private static DepthFormat SelectStencilMode()
        {
            // Check stencil formats
            GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter;
            SurfaceFormat format = adapter.CurrentDisplayMode.Format;
            if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth24Stencil8))
                return DepthFormat.Depth24Stencil8;
            else if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth24Stencil8Single))
                return DepthFormat.Depth24Stencil8Single;
            else if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth24Stencil4))
                return DepthFormat.Depth24Stencil4;
            else if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth15Stencil1))
                return DepthFormat.Depth15Stencil1;
            else
                throw new InvalidOperationException(
                    "Could Not Find Stencil Buffer for Default Adapter");
        }


        public Editor()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredDepthStencilFormat = SelectStencilMode();
            Content.RootDirectory = "Content";

            LevelsOutputPath = Path.GetFullPath(Content.RootDirectory + "\\Maps\\");
            LevelsSourcePath = Path.GetFullPath(".\\");
            if (LevelsSourcePath.IndexOf("Editor\\bin\\x86\\Debug") >= 0)
                LevelsSourcePath = LevelsSourcePath.Replace("Editor\\bin\\x86\\Debug", "BenevolentSun\\Content\\Maps");
            else
                LevelsSourcePath = LevelsSourcePath.Replace("Editor\\bin\\x86\\Release", "BenevolentSun\\Content\\Maps");

            DirectoryInfo levelsOutputDir = new DirectoryInfo(LevelsOutputPath);
            FileInfo[] files = levelsOutputDir.GetFiles("*.*");
            foreach (FileInfo fi in files)
            {
                LevelNames.Add(Path.GetFileNameWithoutExtension(fi.FullName));
                LevelContentNames.Add("Maps/" + Path.GetFileNameWithoutExtension(fi.FullName));
                Console.WriteLine("Maps/" + Path.GetFileNameWithoutExtension(fi.FullName));
            }

            Console.WriteLine(LevelsSourcePath);
            Console.WriteLine(LevelsOutputPath);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            AudioManager.Initialize(this, @"Content\Audio\BenevolentSunAudio.xgs", @"Content\Audio\Wave Bank.xwb", @"Content\Audio\Sound Bank.xsb");

            screenManager = new ScreenManager(this);
            this.Components.Add(screenManager);

            InputManager.Initialize(this);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            screenManager.AddScreen(new LevelMenuScreen());
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            InputManager.Update(gameTime);

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }


        
    }
}
